package com.huawei.codearts.service;

import com.huawei.codearts.dao.CountryDao;
import com.huawei.codearts.model.Country;
import com.huawei.codearts.tools.exception.CustomException;
import com.huawei.codearts.tools.exception.ServiceException;
import com.huawei.codearts.tools.validated.StatusEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class CountryService {

    private final CountryDao countryDao;

    public CountryService(CountryDao countryDao) {
        this.countryDao = countryDao;
    }

    /** 单条数据 */
    public Object getOne(int id) {
        Country country = countryDao.getById(id);

        // 自定义map
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("id", country.getId());
        dataMap.put("name", country.getName());
        dataMap.put("en", country.getEn());
        dataMap.put("state", country.getState());
        dataMap.put("start", country.getStart());

        return dataMap;
    }

    /** 多条数据 */
    public Object getAll() {
        Country country = new Country();
        country.setStart(1);
        return countryDao.getAll(country);
    }

    /** 多条数据 */
    public Map<String, Object> getList() {

        int page = 1;
        int pageSize = 6;

        Country country = new Country();
        country.setStart(1);
        List<Country> items = countryDao.getList(country, page, pageSize);

        List<Object> dataList = new ArrayList<>();
        for(Country item : items) {
            Map<String, Object> dataMap = new ConcurrentHashMap<>();
            dataMap.put("id", item.getId());
            dataMap.put("name", item.getName());
            dataMap.put("en", item.getEn());
            dataMap.put("state", item.getState());
            dataMap.put("start", item.getStart());
            dataList.add(dataMap);
        }

        Map<String, Object> resultList = new ConcurrentHashMap<>();
        resultList.put("count", 10);
        resultList.put("page", page);
        resultList.put("pageSize", pageSize);
        resultList.put("list", dataList);

        return resultList;
    }

    /** 添加 */
    public Map<String, Object> save() {
        Country country = new Country();
        country.setName("新国家");
        country.setEn("xinguojia");
        country.setState("XGJ");
        country.setStart(1);
        int id = countryDao.save(country);
        return Map.of("code", 1, "msg", id > 0 ? "成功" : "失败", "data", Map.of("id", id));
    }

    /** 更新 */
    public Integer update(int id) {

        if (id <= 0) {
//            throw new CustomException(1101, "你不能为空");
            throw new ServiceException(StatusEnum.PARAM_INVALID);
        }

        Country country = new Country();
        country.setId(id);
        country.setName("新国家");
        country.setEn("xinGuoJia");
        country.setState("XGJ");
        country.setStart(0);
        return countryDao.update(country);
    }

    /** 删除 */
    public Integer del(int id) {

        if (id <= 0) {
            throw new CustomException(1101, "id不能为空");
        }

        Country country = new Country();
        country.setId(id);
        return countryDao.remove(country);
    }

}
